package com.nova.bluetooth.helpUtils;
import java.security.MessageDigest;

/**
 * @author Administrator
 * date:2023/7/6/006
 * desc:
 */
public class MD5Util {
    private static final int RANGE_BENGIN = 8;
    private static final int RANGE_END = 24;
    private static byte hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String get32BitLower(String str) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] digests = md.digest();
        byte bytes[] = new byte[digests.length * 2];
        int k = 0;
        for (byte digest : digests) {
            bytes[k++] = hexDigits[digest >>> 4 & 0xf];
            bytes[k++] = hexDigits[digest & 0xf];
        }
        return new String(bytes);
    }

    public static String get32BitUpper(String str) throws Exception {
        return get32BitLower(str).toUpperCase();
    }

    public static String get16BitLower(String str) throws Exception {
        return get32BitLower(str).substring(RANGE_BENGIN, RANGE_END);
    }

    public static String get16BitUpper(String str) throws Exception {
        return get32BitUpper(str).substring(RANGE_BENGIN, RANGE_END);
    }

}

